using System.Linq;
using Engine;
using Engine.Graphics;
using Game;

namespace RuthlessConquest {
    public class WaitingForPlayersDialog : Dialog {
        public static RenderTarget2D GameImageTexture;
        public Client Client;
        public const string fName = "RCWaitingForPlayersDialog";
        public DynamicArray<Player> Players = new();
        public RectangleWidget GameImageRectangle;
        public StackPanelWidget PlayersPanelWidget;
        public ButtonWidget StartButtonWidget;
        public ButtonWidget CancelButtonWidget;
        public ButtonWidget DisconnectButtonWidget;

        public WaitingForPlayersDialog(Client client) {
            Client = client;
            HorizontalAlignment = WidgetAlignment.Center;
            VerticalAlignment = WidgetAlignment.Center;
            Size = new Vector2(600f, 370f);
            ClampToBounds = true;
            Widget[] obj = [
                new RectangleWidget { FillColor = Colors.Back, OutlineColor = Colors.ForeDim, OutlineThickness = 2f }, new InterlaceWidget(), null
            ];
            StackPanelWidget panel1 = new() {
                HorizontalAlignment = WidgetAlignment.Center,
                Direction = LayoutDirection.Vertical,
                Margin = new Vector2(10f, 10f),
                Children = {
                    new CanvasWidget { Size = new Vector2(0f, 5f) },
                    new LabelWidget { Text = LanguageControl.Get(fName, "1"), HorizontalAlignment = WidgetAlignment.Center },
                    new CanvasWidget { Size = new Vector2(0f, float.PositiveInfinity) }
                }
            };
            StackPanelWidget panel2 = new() { HorizontalAlignment = WidgetAlignment.Center };
            StackPanelWidget panel3 = new() {
                Direction = LayoutDirection.Vertical,
                VerticalAlignment = WidgetAlignment.Center,
                Children = {
                    new LabelWidget {
                        Text = LanguageControl.Get(fName, "2"),
                        FontScale = 0.75f,
                        Color = Colors.ForeDim,
                        HorizontalAlignment = WidgetAlignment.Center
                    },
                    new CanvasWidget { Size = new Vector2(0f, 8f) }
                }
            };
            GameImageRectangle = new RectangleWidget {
                Size = new Vector2(128f, 72f), FillColor = Color.White, OutlineColor = Color.Transparent, HorizontalAlignment = WidgetAlignment.Center
            };
            panel3.Children.Add(GameImageRectangle);
            panel2.Children.Add(panel3);
            panel2.Children.Add(new CanvasWidget { Size = new Vector2(50f, 0f) });
            PlayersPanelWidget = new StackPanelWidget { Direction = LayoutDirection.Vertical, VerticalAlignment = WidgetAlignment.Center };
            panel2.Children.Add(PlayersPanelWidget);
            panel1.Children.Add(panel2);
            panel1.Children.Add(new CanvasWidget { Size = new Vector2(0f, 10f) });
            panel1.Children.Add(new CanvasWidget { Size = new Vector2(0f, float.PositiveInfinity) });
            StackPanelWidget panel4 = new() { Direction = LayoutDirection.Horizontal, HorizontalAlignment = WidgetAlignment.Center };
            StartButtonWidget = new BevelledButtonWidget {
                Text = LanguageControl.Get(fName, "3"), Size = new Vector2(250f, 60f), Sound = "RCSounds/Click2", Margin = new Vector2(30f, 0f)
            };
            panel4.Children.Add(StartButtonWidget);
            CancelButtonWidget = new BevelledButtonWidget {
                Text = LanguageControl.Cancel, Size = new Vector2(200f, 60f), Sound = "RCSounds/Click2", Margin = new Vector2(30f, 0f)
            };
            panel4.Children.Add(CancelButtonWidget);
            DisconnectButtonWidget = new BevelledButtonWidget {
                Text = LanguageControl.Get(fName, "4"), Size = new Vector2(250f, 60f), Sound = "RCSounds/Click2", Margin = new Vector2(30f, 0f)
            };
            panel4.Children.Add(DisconnectButtonWidget);
            panel1.Children.Add(panel4);
            obj[2] = panel1;
            Children.Add(obj);
            if (Client.IsGameCreator) {
                DisconnectButtonWidget.IsVisible = false;
                return;
            }
            StartButtonWidget.IsVisible = false;
            CancelButtonWidget.IsVisible = false;
        }

        public override void Update() {
            if (Client.Game.StepModule.IsGameStarted) {
                DialogsManager.ShowDialog(Client.Game.Screen.GameWidget, new CountdownDialog(Client));
                DialogsManager.HideDialog(this);
                return;
            }
            if (GameImageTexture == null) {
                GameImageTexture = new RenderTarget2D(192, 108, 1, ColorFormat.Rgba8888, DepthFormat.None);
            }
            GameImage.FromGame(Client.Game).Draw(GameImageTexture, true);
            GameImageRectangle.Subtexture = new Subtexture(GameImageTexture, Vector2.Zero, Vector2.One);
            bool flag = Client.IsGameCreator
                && Client.Game.PlayersModule.Players.Count(p => p is HumanPlayer) >= Client.Game.CreationParameters.MaxHumanPlayersCount;
            if (StartButtonWidget.IsClicked
                || (StartButtonWidget.IsEnabled && StartButtonWidget.IsVisible && flag)) {
                Client.StartGame();
                StartButtonWidget.IsEnabled = false;
                CancelButtonWidget.IsEnabled = false;
            }
            if (CancelButtonWidget.IsClicked) {
                Client.DisconnectFromGame();
                StartButtonWidget.IsEnabled = false;
                CancelButtonWidget.IsEnabled = false;
            }
            if (DisconnectButtonWidget.IsClicked) {
                Client.DisconnectFromGame();
                DisconnectButtonWidget.IsEnabled = false;
                DialogsManager.HideDialog(this);
                ScreensManager.SwitchScreen("RCMainMenu");
            }
            if (Client.Game.PlayersModule.Players.SequenceEqual(Players)) {
                return;
            }
            PlayersPanelWidget.Children.Clear();
            foreach (Player item in Client.Game.PlayersModule.Players.OrderBy(p => !(p is HumanPlayer) ? 1 : 0)) {
                string text;
                Color color;
                if (item is HumanPlayer) {
                    text = item.IsControllingPlayer ? string.Format(LanguageControl.Get(fName, "5"), item.Name) : item.Name;
                    color = Player.GetColor(item.Faction);
                }
                else {
                    text = string.Format(LanguageControl.Get(fName, "6"), item.Name);
                    color = Colors.ForeDim;
                }
                PlayersPanelWidget.Children.Add(
                    new StackPanelWidget {
                        Direction = LayoutDirection.Horizontal,
                        HorizontalAlignment = WidgetAlignment.Near,
                        VerticalAlignment = WidgetAlignment.Center,
                        Margin = new Vector2(0f, 2f),
                        Children = {
                            new RectangleWidget {
                                Size = new Vector2(26f, 26f),
                                Subtexture = new Subtexture(Ship.GetTexture(item.Faction), Vector2.Zero, Vector2.One),
                                FillColor = Player.GetColor(item.Faction),
                                OutlineColor = Color.Transparent
                            },
                            new CanvasWidget { Size = new Vector2(16f, 0f) },
                            new LabelWidget { Text = text, FontScale = 0.75f, Color = color },
                            new CanvasWidget { Size = new Vector2(8f, 0f) },
                            new RectangleWidget {
                                Size = new Vector2(20f, 20f),
                                Subtexture = new Subtexture(Player.GetPlatformTexture(item.Platform), Vector2.Zero, Vector2.One),
                                IsVisible = Player.GetPlatformTexture(item.Platform) != null,
                                FillColor = Player.GetColor(item.Faction),
                                OutlineColor = Color.Transparent
                            }
                        }
                    }
                );
            }
        }
    }
}